import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
import os
import sys

def plot(prev, pred, upper, C1, C0):
    plt.axis([0, 20, 0, 1.01])
    line1, = plt.plot(prev, '.r-', label='Pl_t')
    line2, = plt.plot(pred, 'hk-', label='Pc_t')
    line3, = plt.plot(upper, 'om-', label='upper_t')    
    line4, = plt.plot(C1, 'xb-', label='Pc_1')
    line5, =plt.plot(C0, '*g-', label='Pc_0')
    plt.legend(handles=[line1, line2, line3, line4, line5])  
    plt.show()

def similarity_stop_policy(l0, trans, guess, slip):
    eps=0.01
    delta=0.95
    
    pc1=(1-slip)*l0+guess*(1-l0)
    total=0
    
    if pc1>0:
        ul0= l0*(1-slip)/(l0*(1-slip)+(1-l0)*guess)
        pl1= ul0+(1-ul0)*trans
        p_cc= (1-slip)*pl1+guess*(1-pl1)
        if abs(pc1-p_cc)<eps:
            total+=pc1

    if pc1<1:
        ul0= l0*slip/(l0*slip+(1-l0)*(1-guess))
        pl1= ul0+(1-ul0)*trans
        p_cbc= (1-slip)*pl1+guess*(1-pl1)
        if abs(pc1-p_cbc)<eps:
            total+=(1-pc1)

    
    return total > delta

def stability_stop_policy(l0,prev_c1,prev_c0, trans, guess, slip):
    eps=0.01
    pc1=(1-slip)*l0+guess*(1-l0)
    
    if pc1>0:
        ul0= l0*(1-slip)/(l0*(1-slip)+(1-l0)*guess)
        pl1= ul0+(1-ul0)*trans
        p_cc= (1-slip)*pl1+guess*(1-pl1)
        
    if pc1<1:
        ul0= l0*slip/(l0*slip+(1-l0)*(1-guess))
        pl1= ul0+(1-ul0)*trans
        p_cbc= (1-slip)*pl1+guess*(1-pl1)
    
#    print(np.abs(p_cc-prev_c1), np.abs(p_cbc-prev_c0))
# should change "or" to "and"     
    return np.abs(p_cc-prev_c1)<eps and np.abs(p_cbc-prev_c0)<eps

def process(response, l0, trans, guess, slip):
    C1=[]
    C0=[]
    prev=[]
    pred=[]
    
    prev_c1=0
    prev_c0=0
    find_sim=1
    find_sta=1
    
    all_correct=np.ones(len(response))
    upper=[]
    
    for i in list(range(len(response))):
        prev.append(l0)       
        pc1=(1-slip)*l0+guess*(1-l0)
        pred.append(pc1)
        

        if (i==0):
            up0=l0
            
        up0= up0*(1-slip)/(up0*(1-slip)+(1-up0)*guess)            
        up_t= up0+(1-up0)*trans
        u_cc= (1-slip)*up_t+guess*(1-up_t)
        upper.append(u_cc) 
        up0=up_t
        
        if pc1>0:
            ul0= l0*(1-slip)/(l0*(1-slip)+(1-l0)*guess)
            pl1_t= ul0+(1-ul0)*trans
            p_cc= (1-slip)*pl1_t+guess*(1-pl1_t)
            C1.append(p_cc)
       

        if pc1<1:
            ul0= l0*slip/(l0*slip+(1-l0)*(1-guess))
            pl1_f= ul0+(1-ul0)*trans
            p_cbc= (1-slip)*pl1_f+guess*(1-pl1_f)
            C0.append(p_cbc)

        if (response[i]==0):
            l0=pl1_f
        if (response[i]==1):
            l0=pl1_t
            
        stop_sim=similarity_stop_policy(l0, trans, guess, slip) 
        if stop_sim and find_sim:
            print('similarity stop at here', i+1,'th problem')
            find_sim=0

        stop_sta=stability_stop_policy(l0, prev_c1, prev_c0, trans, guess, slip) 
        if stop_sta and find_sta:
            print('stability stop at here', i+1,'th problem', np.abs(u_cc-pc1))
            if np.abs(u_cc-pc1)<0.01:
                print ('++ condition is met')
            find_sta=0

        prev_c1=p_cc
        prev_c0=p_cbc
                
    return [prev, pred, upper, C1, C0]

if __name__ == '__main__':
    data = pd.read_csv('english.csv', sep = ',')
    
    stu_id=data[(data['kc']=='egv_cc_9u11')]['s_id'].unique()
    for i in stu_id:
        s2=data[(data['kc']=='egv_cc_9u11') & (data['s_id']==i)]
        print(i, len(s2.correct))
        print(s2.correct.values)

        [prev, pred, upper, C1, C0]=process(s2.correct.values, 0.517, 0.00255, 0.434, 0.205)
        print (C1)
        print (C0)
        #plot(prev, pred, upper, C1, C0)
